﻿using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Neptuno.Component;
using Neptuno.Model;
using System;
using Neptuno.Service.Security;

namespace Neptuno.Service
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class UsuariosService
    {
        AutenticacionComponent Component = new AutenticacionComponent();

        [WebInvoke(UriTemplate = "{id}", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public void AgregarUsuario(string id, Usuarios Usuario)
        {
            Component.AgregarUsuario(Usuario, CurrentUser.Name);
        }

        [WebInvoke(UriTemplate = "AddRol/{username}/{idRol}", Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public bool AgregarRol(string username, string idRol)
        {
            return Component.AsignarRol(username, int.Parse(idRol));
        }

        [WebInvoke(UriTemplate = "{id}", Method = "DELETE", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public bool Delete(string id)
        {
            return Component.EliminarUsuario(id, CurrentUser.Name);
        }

        [WebInvoke(UriTemplate = "UpdatePassword", Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public void ActualizarContrasena(string password, string username, string newPassword)
        {
            Component = new AutenticacionComponent(username, password);

            Component.ActualizarContrasena(newPassword);
        }

        [WebInvoke(UriTemplate = "GetUsuarios", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle=WebMessageBodyStyle.WrappedRequest)]
        public List<string> ActualizarContrasena(string password, string username)
        {
            Component = new AutenticacionComponent(username, password);

            return Component.GetUsuarios();
        }

        [WebGet(UriTemplate = "GetRoles/{username}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public List<Roles> ActualizarContrasena(string username)
        {
            return Component.GetRoles(username);
        }
    }
}